# 工作目录
basepath=~/mrlab

### 编译代码和插件
cd $basepath && rm -rf $basepath/out/*
echo "Compile Master and Worker Program and Move it"
cd $basepath/mrworker/ && go build mrworker.go && mv ./mrworker ../out/
cd $basepath/mrmaster/ && go build mrmaster.go && mv ./mrmaster ../out/
echo "Compile mrapp wc.so"
cd $basepath/mrapp/ && go build -buildmode=plugin wc.go
echo "mv mrapp wc.so to out/"
cp $basepath/mrapp/wc.so $basepath/out/
echo "Copy Input File: in.txt"
cp $basepath/input/*.txt $basepath/out/
echo "ok!"


### 运行程序
cd $basepath/out/

echo "start Worker"
./mrworker wc.so &
./mrworker wc.so &
./mrworker wc.so &

echo "start Master"
./mrmaster ./*.txt &

# 等待所有程序运行完成
wait

echo "MapReduceLite Run Complete"


### 整理结果文件
cd $basepath/out/
echo "Check Output Files"

# 排序汇总
sort mr-out* | grep . > mr-out-all

# 整理输出结果
mkdir out && mv mr-out-* out/ || exit 1
# 整理日志文件
mkdir logs && mv *.log logs/ || exit 1
# 整理输入文件
mkdir inputs && mv ./*.txt inputs/ || exit 1

echo "Check ok"

